package mylist
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class PressTimeOut
	{
		private static var recordTimeOutId:uint;
		private static const recordMax:uint = 3;
		private static var recordPosArr:Vector.<Point> = new Vector.<Point>(recordMax);
		private static var curRecordIndex:int = 0;
		private static var recordedSum:int = 0;
		private static var main:Sprite;
		public static const COMPLETE:String = "COMPLETE";
		
		public function PressTimeOut()
		{
		
		}
		
		public static function start(obj:Sprite):void
		{
			close();
			
			main = obj;
			recordTimeOutId = setTimeout(recordNext, 100);
		}
		
		static private function recordNext():void
		{
			clearTimeout(recordTimeOutId);
			if (main == null || main.stage == null)
			{
				close();
				return;
			}
			recordedSum++;
			recordPosArr[curRecordIndex] = new Point(main.stage.mouseX, main.stage.mouseY);
			//logs.adds(recordPosArr[curRecordIndex]);
			if (recordedSum > recordMax)
			{
				var noMoved:Boolean = true;
				for each (var item:Point in recordPosArr)
				{
					if (Math.abs(item.x - main.stage.mouseX) > 2 || Math.abs(item.y - main.stage.mouseY) > 2)
					{
						noMoved = false;
						break;
					}
				}
				if (noMoved)
				{
					main.dispatchEvent(new Event(COMPLETE));
					close();
					return;
				}
			}
			curRecordIndex++;
			if (curRecordIndex >= recordMax)
				curRecordIndex = 0;
			recordTimeOutId = setTimeout(recordNext, 100);
		}
		
		public static function close():void
		{
			
			for each (var item:Point in recordPosArr)
			{
				if (item)
				{
					item.x = int.MIN_VALUE;
					item.y = int.MIN_VALUE;
				}else {
					item = new Point(int.MIN_VALUE,int.MIN_VALUE);
				}
			}
			clearTimeout(recordTimeOutId);
			main = null;
			curRecordIndex = 0;
			recordedSum = 0;
		}
	}

}